package condition;

import java.awt.Point;
import java.util.ArrayList;
import base.Engine;
import base.Piece;

public class TotalPiecesInColumn extends DropRule {
	private ArrayList<String> applicableTypes, otherTypes;
	private int minimum, maximum;
	
	private boolean isApplicableType(Piece piece) {
		for (String type : this.applicableTypes) {
			if (piece.getRuleType().equals(type)) {
				return true;
			}
		}
		
		return false;
	}
	
	private boolean isOtherType(Piece piece) {
		for (String type : this.otherTypes) {
			if (piece.getRuleType().equals(type)) {
				return true;
			}
		}
		
		return false;
	}
	
	public TotalPiecesInColumn(Engine engine, ArrayList<String> applicableTypes, 
			ArrayList<String> otherTypes, int minimum, int maximum) {
		super(engine);
		this.applicableTypes = applicableTypes;
		this.otherTypes = otherTypes;
		this.minimum = minimum;
		this.maximum = maximum;
	}

	@Override
	public boolean isValidDrop(Piece piece, Point destination) {
		if (this.isApplicableType(piece)) {
			ArrayList<Piece> pieces = this.getEngine().getVirtualBoard().getPiecesByX(destination.x);
			int count = 0;
			
			for (Piece piece2 : pieces) {
				if (this.isOtherType(piece2)) {
					count++;
				}
			}
			
			return this.minimum <= count && count <= this.maximum;
		}
		
		return true;
	}
}
